package com.zx.viewlearn.views.viewDragHelper;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.BitmapShader;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.ComposeShader;
import android.graphics.LinearGradient;
import android.graphics.Paint;
import android.graphics.PorterDuff;
import android.graphics.RadialGradient;
import android.graphics.RectF;
import android.graphics.Shader;
import android.graphics.SweepGradient;
import android.support.annotation.Nullable;
import android.support.v4.view.ViewCompat;
import android.support.v4.widget.ViewDragHelper;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.widget.FrameLayout;

import com.zx.viewlearn.R;

/**
 * Created by zhoux on 2018/1/8.
 * 注释：自定义view onMeasure onDraw学习
 */

public class MyViewViewDragHelper1 extends FrameLayout {
    ViewDragHelper mViewDragHelper;
    View mMenuView, mMainView;


    public MyViewViewDragHelper1(Context context) {
        super(context);
        init(context, null);
    }


    public MyViewViewDragHelper1(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        init(context, attrs);
    }

    @Override
    protected void onFinishInflate() {
        super.onFinishInflate();
        mMenuView = getChildAt(0);
        mMainView = getChildAt(1);
    }

    @Override
    public boolean onInterceptTouchEvent(MotionEvent ev) {
        return mViewDragHelper.shouldInterceptTouchEvent(ev);
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        mViewDragHelper.processTouchEvent(event);
        return true;
    }

    private void init(Context context, AttributeSet attrs) {
        mViewDragHelper = ViewDragHelper.create(this, callBack);
    }


    ViewDragHelper.Callback callBack = new ViewDragHelper.Callback() {
        /**
         * 触摸到哪个view后触发事件
         * @param child
         * @param pointerId
         * @return
         */
        @Override
        public boolean tryCaptureView(View child, int pointerId) {
            return mMainView == child;
        }

        /**
         * 处理垂直滑动
         * @param child
         * @param top
         * @param dy
         * @return
         */
        @Override
        public int clampViewPositionVertical(View child, int top, int dy) {
            return top;//垂直滑动
        }

        /**
         * 处理水平滑动
         * @param child
         * @param left
         * @param dx
         * @return
         */
        @Override
        public int clampViewPositionHorizontal(View child, int left, int dx) {
            return left;
        }


        /**
         * 拖拽结束后调用
         * @param releasedChild
         * @param xvel
         * @param yvel
         */
        @Override
        public void onViewReleased(View releasedChild, float xvel, float yvel) {
            super.onViewReleased(releasedChild, xvel, yvel);

//            if (mMainView.getLeft() < mMenuView.getWidth() + 200) {
//                //
//                mViewDragHelper.smoothSlideViewTo(mMainView, 0, 0);
//                ViewCompat.postInvalidateOnAnimation(MyViewViewDragHelper1.this);
////                invalidate();
//            } else {
//                mViewDragHelper.smoothSlideViewTo(mMainView, mMenuView.getWidth(), 0);
//                ViewCompat.postInvalidateOnAnimation(MyViewViewDragHelper1.this);
////                invalidate();
//            }
        }
    };

    @Override
    public void computeScroll() {
        if (mViewDragHelper.continueSettling(true)) {
            ViewCompat.postInvalidateOnAnimation(this);
//            invalidate();
        }
    }
}
